package com.usac.ventasol;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.ImageButton;

public class Principal extends Activity 
{
	private static final int Inicio = 1;
	private static final int Buscar = 2;
	private static final int Carretilla = 3;
	private static final int Cuenta = 4;
	
	WebView web;
	ImageButton CmdBuscar;
	EditText TxtBuscar;
	String IDevice = "";


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_principal);
		web = (WebView)findViewById(R.id.web);
        CmdBuscar = (ImageButton)findViewById(R.id.CmdBuscar);
        TxtBuscar = (EditText)findViewById(R.id.TxtBuscar);        
        
        TelephonyManager Device = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);                
        IDevice = "AN:" + Device.getDeviceId();               
        
        web.loadUrl("http://maggarzona.webnode.es/");
        web.setWebViewClient(new Paginas());               
        
        CmdBuscar.setOnClickListener(new OnClickListener() 
        {          
	        public void onClick(View v) 
	        {
	        	try 
                {	        			        		
                }
                catch (Exception e) 
                {                	
                	finish();
                }	        	
	        }
	    });
        
        TxtBuscar.setOnFocusChangeListener(new OnFocusChangeListener()
        { 
			public void onFocusChange(View v, boolean hasFocus) 
            {
        		if (hasFocus == true)
        		{         		
        			TxtBuscar.selectAll();        			
        		}
        		else
        		{
        		}        		
			}           
            
        });
        
        TxtBuscar.setOnKeyListener(new OnKeyListener()
        {
			public boolean onKey(View v, int keyCode,KeyEvent event) 
			{
				boolean Res=false;
				if (event.getAction() == KeyEvent.ACTION_DOWN)
				{
					switch (keyCode)
					{
					case KeyEvent.KEYCODE_TAB:
											 
						Res = true;
						break;
					case KeyEvent.KEYCODE_ENTER:
											
						Res = true;
						break;
					case KeyEvent.KEYCODE_BACK:									
						Res = true;
						break;
					default: 
						Res = false;							
					}
				}
				return Res;
			}
        	
        });
	}

	@Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        //getMenuInflater().inflate(R.menu.activity_main, menu);
    	super.onCreateOptionsMenu(menu);
    	menu.add(Menu.NONE, Inicio, Menu.NONE, "Inicio").setIcon(R.drawable.inicioact);    	
        menu.add(Menu.NONE, Buscar, Menu.NONE, "Buscar").setIcon(R.drawable.buscar);
        menu.add(Menu.NONE, Carretilla, Menu.NONE, "Carretilla").setIcon(R.drawable.carretilla);
        menu.add(Menu.NONE, Cuenta, Menu.NONE, "Mi Cuenta").setIcon(R.drawable.cuenta);               
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) 
    {
        switch (item.getItemId()) {
        	case Inicio:
        		//Inicio();
            return true;
	        case Buscar:
	        	try 
                {
        			Intent intento = new Intent();	                	        		
        	        intento.setClass(Principal.this, Buscar.class);
        	        startActivityForResult(intento,1);	 
                }
                catch (Exception e) 
                {
                	finish();
                }
	            return true;
	        case Carretilla:
	        	try 
                {
	        		Intent intento = new Intent();	                	        		
        	        intento.setClass(Principal.this, Carretilla.class);
        	        startActivityForResult(intento,1);
                }
                catch (Exception e) 
                {
                	finish();
                }
	            return true;	        	        
	        case Cuenta:
	        	try 
                {	        		
	        		Intent intento = new Intent();	                	        		
        	        intento.setClass(Principal.this, Cuenta.class);
        	        startActivityForResult(intento,1);
                }
                catch (Exception e) 
                {
                	finish();
                }	        	
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
        }
    }
    
    private class Paginas extends WebViewClient
    {    	
	    public boolean shouldOverrideUrlLoading(WebView view, String url)
    	{
    		view.loadUrl(url);
    		return true;
	    }
    }
    
    public void onActivityResult(int requestCode, int resultCode, Intent data)
	{     
		  super.onActivityResult(requestCode, resultCode, data); 		  			 		  		 
		  try
		  {
		     
		  }
		  catch(Exception Ex)
		  {
			  
		  }
     }
}
